require('./services/mongo');
const Topic = require('./models/topic');
const Crawler = require('./services/crawler');

(async () => {
    let count = 0
    for(let i=0; i<1000; i +=25){
        let results = await Crawler.fetchSingleDoubanList(i)
        for(let j=0; j<results.length; j++){
            let singledata = results[j]
            let foundTopic = await Topic.findOne({url: results[j].url})
                .then(r => r)
                .catch(e => {
                    console.log(e)
                })
            if(foundTopic){
                count ++
            } else {
                if(isNear(singledata.title)){
                    // console.log(r.url)
                    let detailPic = await Crawler.fetchSingleDoubanTopic(singledata.url)
                    Object.assign(singledata, detailPic)
                }
                // let detailPic = await Crawler.fetchSingleDoubanTopic(singledata.url)
                // Object.assign(singledata, detailPic)
                await Topic.create(singledata).then(r => r).catch(e => {console.log(e)})
            }
            // if(isNear(r.title)){
            //     console.log(r.url)
            //     let results = await Crawler.fetchSingleDoubanTopic(r.url)
            //     console.log(results)
            // }
        }
        // await Crawler.fetchSingleDoubanTopic(results[0].url)
    }
    console.log(`count: ${count}`)
})()
    .then( res => {
        console.log('done')
        process.exit(0)
    })
    .catch(e => {
        console.log(e)
        process.exit(1)
    })

    function isNear(location){
        return location.indexOf('陆家嘴') > -1
    }